package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.LoginJsonUtils;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.PhotoUtils;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/*
学生编辑资料
 */
public class MaterialUpload extends BaseActivity {
    private EditText edit_nickname, edit_compellation;
    private String nickname, compellation;
    private ImageButton imageBtn_uploadPic;
    private ImageView imageView;
    private RadioGroup radiogroup;
    private RadioButton radioButton_man, radioButton_woman;
    private String sexID = "";
    private SharedPreferences sharedPreferences;

    private ProgressDialog progressDialog;
    private boolean flag = false;

    private ArrayList<String> pathList;
    private static final int REQUEST_IMAGE = 2;
    private String picPath;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_material_upload);
        init();
    }

    private void init() {
        sharedPreferences = getSharedPreferences("data_dyk", 0);
        edit_nickname = (EditText) findViewById(R.id.bjzl_nickname);
        edit_compellation = (EditText) findViewById(R.id.bjzl_compellation);
        imageBtn_uploadPic = (ImageButton) findViewById(R.id.imageBtn_uploadPic);
        imageView = (ImageView) findViewById(R.id.imageView);
        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioButton_man = (RadioButton) findViewById(R.id.radioButton_man);
        radioButton_woman = (RadioButton) findViewById(R.id.radioButton_woman);


    }

    //返回按钮
    public void back(View view) {
        finish();
    }

    //上传
    public void uploading(View view) {
        switch (view.getId()) {
            case R.id.btn_upload:
                if (TextUtils.isEmpty(edit_nickname.getText()) ||
                        TextUtils.isEmpty(edit_compellation.getText()) ||
                        imageView == null ||
                        radiogroup == null) {
                    Toast.makeText(this, "请完善资料", Toast.LENGTH_SHORT).show();
                } else {
                    progressDialog = ProgressDialog.show(this, null, "正在上传....", true, false);
                    int btnid = radiogroup.getCheckedRadioButtonId();
                    if (btnid == radioButton_man.getId()) {
                        sexID = "1";
                    } else if (btnid == radioButton_woman.getId()) {
                        sexID = "0";
                    } else {
                        Toast.makeText(this, "您还没选择性别", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    final String sessionid = CookieHelper.getSessionID(sharedPreferences);
                    final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.STUDENTMATERIALUPLOAD;//学生上传资料接口
                    RequestParams params = new RequestParams(upLoadUrl);
                    nickname = edit_nickname.getText().toString();
                    compellation = edit_compellation.getText().toString();
                    params.addHeader("cookie", sessionid);
                    params.addBodyParameter("aliasname", nickname);
                    params.addBodyParameter("memname", compellation);
                    params.addBodyParameter("gender", sexID);
                    params.addBodyParameter("avatar", new File(picPath));
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            Log.e("test", "上传结果" + result);
                            Message message = handler.obtainMessage();
                            message.what = 1;
                            message.obj = result;
                            handler.sendMessage(message);
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                }
                break;
        }

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String state = (String) msg.obj;
                    Log.e("test", "上传结果" + state);
                    HashMap<String, Object> jsonFromLogin = LoginJsonUtils.getJsonFromLogin(state);
                    flag = (boolean) jsonFromLogin.get("success");
                    if (flag) {
                        Toast.makeText(MaterialUpload.this, "学生资料上传成功", Toast.LENGTH_SHORT).show();

                        setResult(3);
                        progressDialog.dismiss();
                        finish();
                    } else {
                        Toast.makeText(MaterialUpload.this, "学生资料上传失败", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }
                    break;
            }
        }

    };

    //选择图片
    public void phone(View view) {
        int maxNum = 1;
        Intent intent = new Intent(MaterialUpload.this, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        if (pathList != null && pathList.size() > 0) {
            intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, pathList);
        }
        startActivityForResult(intent, REQUEST_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE) {
            if (resultCode == Activity.RESULT_OK) {
                pathList = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                for (String s : pathList) {
                    PhotoUtils photoUtils3 = new PhotoUtils();
                    PhotoUtils.CompressOptions options3 = new PhotoUtils.CompressOptions();
                    options3.uri = Uri.fromFile(new File(s));
                    Bitmap bitmap3 = photoUtils3.compressFromUri(this, options3);
                    imageView.setImageBitmap(bitmap3);
                    picPath = s;
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
